﻿using System;
using System.Text;
using System.Data;
using System.Web.UI.WebControls;
using PlugNT.Safe;
using PlugNT.Cms.Core;
using PlugNT.Cms.BLL;
using PlugNT.Cms.Model;
using PlugNT.Common;
using PlugNT.Cms.Page;

namespace PlugNT.Cms.Web.Admin.Pre_User
{
    public partial class Admin : AdminPage
    {
        private PlugNT.Cms.BLL.User curUser = new PlugNT.Cms.BLL.User();
        private string username = "";
        private int pageSize = 15;
        public int colspan = 1;

        protected void Page_Load(object sender, EventArgs e)
        {
            //权限检测
            this.CheckPower();

            username = (Request.QueryString["u"] == null) ? "" : Request.QueryString["u"].Trim();
            if (!IsPostBack)
            {
                string type = (Request.QueryString["t"] == null) ? "" : Request.QueryString["t"].Trim();
                string id = (Request.QueryString["id"] == null) ? "" : Request.QueryString["id"].Trim();
                string ids = (Request.QueryString["ids"] == null) ? "" : Request.QueryString["ids"].Trim();
                if (type == "delete")
                {
                    if (id != "")
                    {
                        Delete_user(int.Parse(id));
                    }
                    if (ids != "")
                    {
                        Delete_users(ids.Split(','));
                    }
                }

                if (type == "commend")
                {
                    if (id != "")
                    {
                        if (Request["commend"] == "1") { Commend_user(Convert.ToInt32(Request["id"]), false); }
                        else { Commend_user(Convert.ToInt32(Request["id"]), true); }

                    }
                }
                else
                {
                    try
                    {
                        Repeater_admin_bind();
                    }
                    catch (Exception ex)
                    {
                        Response.Redirect("../Message.aspx?message=数据绑定错误！" + ex.Message + "&z=no");
                    }
                }
            }
            colspan = int.Parse(hidColspan.Value);
        }



        //删除一条数据
        void Delete_user(int uid)
        {
            if (curUser.Delete(uid))
            {
                Response.Redirect("Admin.aspx");
            }
            else
            {
                Response.Redirect("../Message.aspx?message=删除用户失败！&z=no");
            }
        }

        void Commend_user(int uid, bool commend)
        {
            if (curUser.Commend(uid, commend))
            {
                Response.Redirect("Admin.aspx");
            }
            else
            {
                Response.Redirect("../Message.aspx?message=删除用户失败！&z=no");
            }
        }

        //批量删除
        void Delete_users(string[] uids)
        {
            int nub = 0;
            foreach (string uid in uids)
            {
                if (uid != "0")
                {
                    if (curUser.Delete(int.Parse(uid)))
                    {
                        nub += 1;
                    }
                }
            }
            Response.Redirect("../Message.aspx?message=批量删除<b>" + nub + "</b>条信息成功！&z=yes");
        }


        #region 数据绑定
        void Repeater_admin_bind()
        {

            string gopage = (Request.QueryString["page"] == null) ? "0" : Request.QueryString["page"].Trim();
            int page = Convert.ToInt32(gopage);

            int recordCount = 0;
            string columns = PlugNT.Custom.WebConfig.UsersSelectFields.Trim();
            string andWhere = (username == "") ? "" : " and " + PlugNT.Custom.WebConfig.UsersUserName + "='" + StringHelper.SqlFilter(username) + "'";
            DataTable dt = curUser.GetList(pageSize, (page - 1), andWhere, columns, out recordCount);
            litPager.Text = PlugNT.Common.Pager.Pagination(recordCount, pageSize, page, "Admin.aspx", GetBindUrl(), "", true, true);

            //输出内容
            string tmpTitleString = "<td align=\"center\" {0} height=\"25\" nowrap=\"nowrap\" bgcolor=\"#DDE9FF\">{1}</td>";
            string tmpDataString = "<td align=\"center\" nowrap=\"nowrap\" bgcolor=\"#F4FBFF\">{0}</td>";
            StringBuilder sbHtml = new StringBuilder();
            //头部
            int colCount = dt.Columns.Count;
            int percent = 100 / colCount;
            string idName = PlugNT.Custom.WebConfig.UsersIdName;
            string userName = PlugNT.Custom.WebConfig.UsersUserName;
            string commend = "commend";
            sbHtml.AppendLine("<tr>");
            sbHtml.AppendLine(string.Format(tmpTitleString, "width=\"25\"", ""));
            for (int i = 0; i < colCount; i++)
            {
                string colName = dt.Columns[i].ColumnName;
                sbHtml.AppendLine(string.Format(tmpTitleString, "width=\"" + percent + "%\"", colName));
            }
            sbHtml.AppendLine(string.Format(tmpTitleString, "width=\"100\"", "推荐"));

            sbHtml.AppendLine(string.Format(tmpTitleString, "width=\"100\"", "操作"));
            sbHtml.AppendLine("</tr>");
            //内容部分
            foreach (DataRow dr in dt.Rows)
            {
                sbHtml.AppendLine("<tr>");
                sbHtml.AppendLine(string.Format(tmpDataString, "<input name=\"iptbox\" type=\"checkbox\" value=\"" + dr[idName].ToString() + "\" />"));
                for (int i = 0; i < colCount; i++)
                {
                    sbHtml.AppendLine(string.Format(tmpDataString, dr[i].ToString()));
                }
                string str = ("1".Equals(Convert.ToString(dr[commend]))) ? "<font color='#FF3300' title='点击取消推荐'><img src='../images/cms-ico7.gif' border='0' /></font>" : "<font color='#FF3300' title='点击推荐'><img src='../images/cms-ico6.gif' border='0' /></font>";

                sbHtml.AppendLine(string.Format(tmpDataString, "<a href=\"?t=commend&id=" + dr[idName].ToString() + "&commend=" + Convert.ToString(dr[commend]) + "\">" + str + " </a>"));

                sbHtml.AppendLine(string.Format(tmpDataString,
                    "<a target=_blank href=\"/home" + dr[idName].ToString() + ".aspx\">主页</a> | <a href=\"EditUser.aspx?t=edit&id=" + dr[idName].ToString() +
                    "\">编辑</a> | <a href=\"?t=delete&id=" + dr[idName].ToString() +
                    "\" onClick=\"return window.confirm('确定删除 [" + dr[idName].ToString() + "] 吗?');\">删除</a>"));
                sbHtml.AppendLine("</tr>");
            }
            hidColspan.Value = (colCount + 2).ToString();
            litDataString.Text = sbHtml.ToString();

            //Repeater_admin.DataSource = dt;
            //Repeater_admin.DataBind();
        }
        #endregion




        private string GetBindUrl()
        {
            //return "power=" + power + "&page";
            return "page";
        }



    }
}